package com.wanda.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wanda.model.Apartment_LayoutModel;
import com.wanda.model.BuildModel;
import com.wanda.service.Apartment_LayoutService;
import com.wanda.util.DateFormat;

@Controller
@RequestMapping(value="/ht/apar")
public class Apartment_LayoutController {

	@Autowired
	private Apartment_LayoutService apartment_LayoutService;

	@RequestMapping(value="/addApar.do")
	public @ResponseBody Map<String,Object> add(String apartment_layout,String delete_state){
		
		System.out.println("进入添加户型controller");
		
		Apartment_LayoutModel model = new Apartment_LayoutModel();
		
		model.setApartment_layout(apartment_layout);
		model.setDelete_state("0");
		
		apartment_LayoutService.insertApar(model);

		Map<String,Object> map = new HashMap<String,Object>();
		
		map.put("msg","成功");
		
		return map;
	}
	
	//修改户型
	@RequestMapping(value="/updateApar.do")
	public String update(Apartment_LayoutModel model){
		
		System.out.println("进入修改户型");
		model.setUpdate_time(DateFormat.getNowDate());
		
		apartment_LayoutService.updateApar(model);
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("aparList",map);
		
		return "redirect:/ht/apar/selectAll.do";
		
	}
	
	//软删除一条记录
	@RequestMapping(value="/deleteApar.do")
	public String delete(Apartment_LayoutModel model){
		
		System.out.println("进入删除户型");
		model.setDelete_time(DateFormat.getNowDate());
		model.setApartment_layout_id(model.getApartment_layout_id());
		
		int apar=apartment_LayoutService.deleteApar(model);
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("msg", apar);
		
		return "redirect:/ht/apar/selectAll.do";
	}
	
	//查询单条记录
	@RequestMapping(value="/selectById.do")
	public ModelAndView selectById(String id){
		
		System.out.println("进入单条查询");
		
		ModelAndView model = new ModelAndView();
		Apartment_LayoutModel apar = null;	
		try{
			
			apar=apartment_LayoutService.selectById(id);		
			model.setViewName("/ht/updateApartment_Layout.jsp");
			model.addObject("apar",apar);
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return model;
	}
	
	//查询户型所有信息
	@RequestMapping(value="/selectAll.do")
	 public ModelAndView showAll(){
		
		System.out.println("进入单元查询");
		
		ModelAndView model1 = new ModelAndView();
		
       	List<Apartment_LayoutModel> apar =null;
     	try {
   		
   		apar = apartment_LayoutService.selectAll(); 
   		model1.setViewName("/ht/listApartment_Layout.jsp");
   		model1.addObject("apars", apar); 
   		
   	}catch(Exception ex){  
   		ex.printStackTrace();
   	}
   	 return model1;
	}
	
	//遍历房间表里需要的字段并返回(户型名字)
	@RequestMapping(value="/selectAlls.do")
		public @ResponseBody Map<String,Object> showAlls(String id){
			
			Map<String,Object> map = new HashMap<String,Object>();

			List<Apartment_LayoutModel> model = null;
			model=apartment_LayoutService.selectAll();
			map.put("msg",model);
			
			return map;	
		}
}
